<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * @todo 店铺管理
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2015-8-17
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class ShopController extends AdminController{
	
	/**
	 * @todo 店铺管理
	 * @author sap
	 */
	public function index(){
		$search_text = trim(strip_tags(I('search_text')));//搜索内容
		$map = array();//初始化查询条件
		//查询条件
		if($search_text != '' && $search_text != null){
			$map['shop_name'] = array('like', '%'.(string)$search_text.'%');
		}
		//列表
		$map['status']=array('gt',-1);
		$list = $this->lists('Shop',$map,'shop_id desc','shop_id,shop_name,merchant_id,conf_category_id,shop_category_id,status');
		int_to_string($list,array(
	        'status'=>array('-1'=>'删除','0'=>'禁用','1'=>'启用')
	    ));
		$this->assign('_list',$list);
		$this->assign('meta_title','店铺管理');
		$this->display('index');
	}
	
	/**
	 * @todo 添加店铺
	 * @author sap
	 * @update cruise <jiangyu@hrbjjhh.com>
	 * @todo 添加店铺品类，商品品类选项
	 */
	public function add(){
		if(IS_POST){
			$goods_category_id=I('goods_category_id');
			$this->update($goods_category_id);
		}else{
			$this->getConfCategoryList();//品类列表
			$this->getMerchantList();//商户列表
			//取出商品品类
			$first_Category = D('Admin/ShopGoodsCategory')->firstGoodsCategoryShow('goods_category_name,goods_category_id');
			//取出店铺品类
			$shopCategoryList = D('Admin/ServiceApi')->getShopCategory();
			//标志不是复选选中
			foreach ($first_Category as $key=>$val){
				$first_Category[$key]['checked'] = 0;
			}
			
			$this->assign('list',null);
			$this->assign('first_category',$first_Category);
			$this->assign('shopCategoryList',$shopCategoryList);
			$this->assign('meta_title','新增店铺');
			$this->assign('meta_title_c','店铺管理');
			$this->display('add');
		}
	}
	
	/**
	 * @todo 编辑店铺
	 * @author sap
	 * @update Cruise <jiangyu@hrbjjhh.com>
	 * @todo 添加店铺品类，商品品类选项
	 */
	public function edit(){
		if(IS_POST){
			$goods_category_id=I('goods_category_id');
			$this->update($goods_category_id);
		}else{
			$this->getConfCategoryList();//品类列表
			$this->getMerchantList();//商户列表
			//隐藏标签传递shop_id
			$shop_id = (int)I('shop_id');
			if($shop_id == 0){
				$this->error('请正确操作');
			}
			//取出shop表中的数据
			$info = D('Shop')->shopShow($shop_id,true);
			//取出所有店铺品类
			$shopCategoryList = D('ServiceApi')->getShopCategory();
			//取出iweb_shop_goods_category_shop表中对应shop_id的goods_category_id
			$maps['shop_id'] = $shop_id;
			$goods_category_id = M('shopGoodsCategoryShop')->where($maps)->field('goods_category_id')->select();
			//取出iweb_shop_goods_category表中所有一级品类
			$goods_category = D('Admin/ShopGoodsCategory')->firstGoodsCategoryShow('goods_category_name,goods_category_id');
			/* 判断是否有商户和经营品类对应 */
			if(!empty($goods_category_id)){
				//商户和店铺品类有对应时
				$goods_category_ids = array_column($goods_category_id, 'goods_category_id');
				foreach ($goods_category as $key=>$val){
					$goods_category[$key]['checked'] = in_array($val['goods_category_id'],$goods_category_ids)?1:0;
				}
			}else{
			    if(!empty($goods_category)){
			        foreach ($goods_category as $key=>$val){
			            $goods_category[$key]['checked'] = 0;
			        }
			    }
			} 
			//显示
			$this->assign('list',$info);
			$this->assign('shopCategoryList',$shopCategoryList);
			$this->assign('first_category',$goods_category);
			$this->assign('meta_title','更新店铺');
			$this->assign('meta_title_c','店铺管理');
			$this->display('add');
		}
	}
	
	/**
	 * @todo 添加/编辑店铺(仅供内部调用)
	 * @author sap
	 * @update Cruise <jiangyu@hrbjjhh.com>
	 * @todo 添加品类选项
	 */
	private function update($goods_category_id){
		$shop_id = (int)I('shop_id');
		$goods_category_ids = $goods_category_id;
		$obj = D('Shop');
		$update = $obj->update();
		if(!$update){
			$this->error($obj->getError());
		}else{
			$name = $obj->changeShopCategory($update,$goods_category_ids);
		}
		if($shop_id == 0){
			action_log('addShop', 'Shop', $update, UID);
		}else{
			action_log('editShop', 'Shop', $shop_id, UID);
		}
		$this->success('操作成功',U('Shop/index'));
	}
	
	/**
	 * @todo 品类列表
	 * @author sap
	 */
	private function getConfCategoryList(){
		$conf_category_list = D('ConfCategory','Service')->getConfCategoryList(array('status'=>array('gt',-1)));
		$this->assign('conf_category_list',$conf_category_list);
	}
	
	/**
	 * @todo 商户列表
	 * @author sap
	 */
	private function getMerchantList(){
		$merchant_list = D('Merchant','Service')->getMerchantList(array('status'=>array('gt',-1)));
		$this->assign('merchant_list',$merchant_list);
	}
	
	/**
	 * @todo 活动列表
	 * @author sap
	 */
	private function getActivityList(){
		$activity_list = D('Activity','Service')->getActivityList(0);//活动列表
		$this->assign('activity_list',$activity_list);
	}
	
	/**
	 * @todo 商户状态修改
	 * @param string $method 标识
	 * @author sap
	 * @update Cruise <jiangyu@hrbjjhh.com>
	 * @todo  添加行为日志
	 */
	public function changeStatus($method=null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if(empty($id)) {
			$this->error('请选择要操作的数据!');
		}
		$map[D('Shop')->getPk()] = array('in',$id);
		switch (strtolower($method) ){
			case 'forbiduser':
				$this->forbid('Shop', $map );
				action_log('disableShop', 'Shop', $id, UID);
				break;
			case 'resumeuser':
				$this->resume('Shop', $map );
				action_log('enableShop', 'Shop', $id, UID);
				break;
			case 'deleteuser':
				$obj_goods = D('Admin/Shop');
				/* 删除店铺时，验证是否绑定店铺品类，商品品类，商品 */
				if($obj_goods->shopCategoryJudge($id) === false){
					$this->error($obj_goods->getError());
				}
				if($obj_goods->shopGoodsCategoryJudge($id) === false){
					$this->error($obj_goods->getError());
				}
				if($obj_goods->shopGoodsJudge($id) === false){
					$this->error($obj_goods->getError());
				}
				
				//删除店铺活动关系
				$obj = D('Admin/ShopActivity');
				if(!$obj->shopActivityDel(array('shop_id'=>array('in',$id)))){
					$this->error($obj->getError());
				}
				action_log('deleteShop', 'Shop', $id, UID);
				$this->delete('Shop', $map );
				//删除缓存
				S('sys_shop_', null);
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * @todo 展会店铺管理
	 * @author sap
	 */
	Public function activityShopList(){
		$this->getActivityList();//活动列表
		$map = array();//初始化查询条件
		//查询条件
		$search_text = trim(strip_tags(I('search_text')));//搜索内容
		if($search_text != '' && $search_text != null){
			$map['shop.shop_name'] = array('like', '%'.(string)$search_text.'%');
		}
		$activity_id = (int)I('activity_id');//活动ID
		if($activity_id !=0){
			$map['shop_activity.activity_id'] = $activity_id;
		}
		//列表
		$map['shop.status']=array('gt',-1);
		$field = 'shop_activity_id,shop_id,shop_name,merchant_id,conf_category_id,activity_id,'
		    .'booth_number,cooperation_year,is_sign,code_id,create_time';
		$list = $this->lists(D('ShopActivityView'),$map,'shop.shop_id desc',$field);
		int_to_string($list,array(
		    'cooperation_year'=>array('0'=>'否','1'=>'是'),
		    'is_sign'=>array('0'=>'否','1'=>'是')
		));
		//显示
		$this->assign('_list',$list);
		$this->assign('meta_title','展会店铺管理');
		$this->display('activity_school');
	}
	
	/**
	 * @todo 添加活动店铺
	 * @author sap
	 */
	public function addActivity(){
		if(IS_POST){
			$obj = D('Admin/ShopActivity');
			if(!$obj->update()){
				$this->error($obj->getError());
			}
			$this->success('操作成功',U('Shop/activityShopList'));
		}else{
			$this->getActivityList();//活动列表
			$shop_list = D('Shop')->getShopList(array('status'=>array('gt',-1)),'shop_id,shop_name');//店铺列表
			//显示
			$this->assign('shop_list',$shop_list);
			$this->assign('meta_title_c','展会店铺管理');
			$this->assign('meta_title','新增活动店铺');
			$this->display('add_activity');
		}
	}
	
	/**
	 * @todo 编辑活动店铺
	 * @author sap
	 */
	public function editActivity(){
	    if(IS_POST){
	        $obj = D('Admin/ShopActivity');
	        if(false === $obj->update()){
	            $this->error($obj->getError());
	        }
	        $this->success('操作成功',U('Shop/activityShopList'));
	    }else{
	        $id = (int)I('get.id');
	        if($id == 0){
	            $this->error('活动店铺id错误');
	        }
	        $map['shop_activity_id'] = $id;
	        $shop_activity_info = M('shop_activity')->where($map)->find();
	        if(empty($shop_activity_info)){
	            $this->error('参展店铺信息不存在');
	        }
	        $this->assign('shop_activity_info',$shop_activity_info);
	        
	        $this->getActivityList();//活动列表
	        $shop_list = D('Shop')->getShopList(array('status'=>array('gt',-1)),'shop_id,shop_name');//店铺列表
	        //显示
	        $this->assign('shop_list',$shop_list);
	        $this->assign('meta_title_c','展会店铺管理');
	        $this->assign('meta_title','编辑活动店铺');
	        $this->display('add_activity');
	    }
	}
	
	/**
	 * @todo 删除活动与店铺的关系
	 * @author sap
	 */
	public function getShopActivityDel(){
		//获取
		$shop_id = (int)I('shop_id');
		$activity_id = (int)I('activity_id');
		if($shop_id == 0 || $activity_id == 0){
			$this->error('店铺或活动不正确');
		}
		//删除操作
		$obj = D('Admin/ShopActivity');
		if(!$obj->shopActivityDel(array('shop_id'=>$shop_id,'activity_id'=>$activity_id))){
			$this->error($obj->getError());
		}else{
			$this->success('删除成功');
		}
	}
	
	/**
	 * @todo 根据活动ID调取店铺列表（公共方法）不受权限控制
	 * @param int $activity_id 活动ID号
	 * @author sap
	 */
	public function getShopActivityList($activity_id='',$data_type=''){
		//获取
		if($activity_id == ''){
			$activity_id = (int)I('activity_id');
		}
		if($data_type == ''){
			$data_type = (int)I('data_type');
		}
		//读取列表
		if($activity_id != 0){
			$map = array();
			$map['shop.status'] = array('gt',-1);
			$map['shop_activity.activity_id'] = $activity_id;
			$shop_list = D('Shop')->getShopActivityList($map,'shop_id,shop_name');
		}else{
			$shop_list = null;
		}
		//返回值
		$data= array();
		switch ($data_type){
			case 0:
				return $shop_list;
				break;
			case 1:
				if(empty($shop_list)){
					$data['status'] = 0;
					$data['info'] = '没有数据';
				}else{
					$data['status'] = 1;
					$data['data'] = $shop_list;
				}
				$this->ajaxReturn($data);
				break;
			default:
				$this->error('参数有误');
				break;
		}
	}
	
	/**
	 * @todo 根据活动调取商户列表（公共方法）不受权限控制
	 * @author sap
	 */
	public function getActivityMerchant($activity_id = '',$data_type=''){
		//获取
		if($activity_id == ''){
			$activity_id = (int)I('activity_id');
		}
		if($data_type == ''){
			$data_type = (int)I('data_type');
		}
		//获取商户数据
		if($activity_id != 0){
			$merchant_list = D('Shop')->getActivityMerchant($activity_id);
		}else{
			$merchant_list = null;
		}
		//返回值
		$data= array();
		switch ($data_type){
			case 0:
				return $merchant_list;
				break;
			case 1:
				if(empty($merchant_list)){
					$data['status'] = 0;
					$data['info'] = '没有数据';
				}else{
					$data['status'] = 1;
					$data['data'] = $merchant_list;
				}
				$this->ajaxReturn($data);
				break;
			default:
				$this->error('参数有误');
				break;
		}
		
	}
	
	/**
	 * @todo 根据活动商户调取店铺（公共方法）不受权限控制
	 * @author sap
	 */
	public function getMerchantShop($activity_id='',$merchant_id='',$data_type=''){
		//获取
		if($activity_id == ''){
			$activity_id = (int)I('activity_id');
		}
		if($merchant_id == ''){
			$merchant_id = (int)I('merchant_id');
		}
		if($data_type == ''){
			$data_type = (int)I('data_type');
		}
		//获取店铺数据
		if($merchant_id != 0 && $activity_id != 0){
			$shop_list = D('Shop')->getMerchantShop($activity_id,$merchant_id);
		}else{
			$shop_list = null;
		}
		//返回值
		$data= array();
		switch ($data_type){
			case 0:
				return $shop_list;
				break;
			case 1:
				if(empty($shop_list)){
					$data['status'] = 0;
					$data['info'] = '没有数据';
				}else{
					$data['status'] = 1;
					$data['data'] = $shop_list;
				}
				$this->ajaxReturn($data);
				break;
			default:
				$this->error('参数有误');
				break;
		}
	}
	
	/**
	 * @todo 店铺品类
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function shopCategory(){
		$search_text = trim(strip_tags(I('search_text')));//搜索内容
		$map = array();//初始化查询条件
		if($search_text != '' && $search_text != null){
			$map['shop_category_name'] = array('like', '%'.(string)$search_text.'%');
		}
		//列表
		$map['status'] = array('gt',-1);
		$list = $this->lists('shop_category',$map,'sort desc,shop_category_id desc','shop_category_id,shop_category_name,sort');
		
		$this->assign('_list',$list);
		$this->assign('meta_title','店铺品类管理');
		$this->display('shopCategory');
	}
	
	/**
	 * @todo 店铺品类
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function addShopCategory(){
		if(IS_POST){
			$this->updateShopCategory();
		}else{
			$this->assign('list',null);//功能用一个页时需要给值null
			$this->assign('meta_title','新增店铺品类');
			$this->assign('meta_title_c','店铺品类');
			$this->display('shopCategoryAdd');
		}
	}
	
	/**
	 * @todo 添加/编辑店铺品类(仅供内部调用)
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	private function updateShopCategory(){
		$shop_category_id = (int)I('shop_category_id');//更新时ID值
		$obj = D('Admin/ShopCategory');
		$id = $obj->update();
		if($id === false){//fang update
			$this->error($obj->getError());
		}else{
			if($shop_category_id == 0){
				//新增行为操作
				action_log('addShopCategory', 'ShopCategory', $id, UID);
			}else{
				//更新行为操作
				action_log('editShopCategory', 'ShopCategory', $shop_category_id, UID);
			}
			$this->success('操作成功',U('shopCategory'));
		}
	}
	
	/**
	 * @todo 店铺品类
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function editShopCategory(){
		if(IS_POST){
			$this->updateShopCategory();
		}else{
			//编辑时，店铺品类详细信息
			$shop_category_id = (int)I('shop_category_id');
			if($shop_category_id == 0){
				$this->error('店铺品类id错误');
			}
			$obj = D('Admin/ShopCategory');
			$info = $obj->ShopCategoryShow($shop_category_id,'shop_category_id,shop_category_name,sort');
			if(empty($info)){
				$this->error($obj->getError());
			}
			//渲染显示
			$this->assign('list',$info);
			$this->assign('meta_title','更新店铺品类');
			$this->assign('meta_title_c','店铺品类管理');
			$this->display('shopCategoryAdd');
		}
	}
	
	/**
	 * @todo 店铺品类状态修改
	 * @param string $method 标识
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function changeShopCategory($method = null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if(empty($id)) {
			$this->error('请选择要操作的数据!');
		}
		$obj = D('Admin/ShopCategory');
		$map[$obj->getPk()] = array('in',$id);
		switch (strtolower($method) ){
			case 'forbid':
				$this->forbid($obj, $map );
				break;
			case 'resume':
				$this->resume($obj, $map );
				break;
			case 'delete':
				if($obj->judgeShopCategory($id) === false){
					$this->error($obj->getError());
				}else{
					action_log('deleteShopCategory', 'ShopCategory', $id, UID);
					$this->delete($obj, $map );
				}
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * @todo 商品品类列表(1，2，3，4级....)
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function goodsCategory(){
		$goods_category_pid  = I('get.pid',0);
		$goods_category_name = strip_tags(trim(I('get.search_text')));
		if($goods_category_pid > 0){
			$data = M('ShopGoodsCategory')->where("goods_category_id={$goods_category_pid}")->field(true)->find();
			if($data['goods_category_pid'] > 0){
				$data_up = M('ShopGoodsCategory')->where("goods_category_id={$data['goods_category_pid']}")->field(true)->find();
				$this->assign('data_up',$data_up);
			}else{
				$this->assign('data_up',NULL);
			}
			$this->assign('data',$data);
		}else{
			$this->assign('data',NULL);
		}
		//标志位筛选
		$map['status'] = 1;
		//**层级筛选
		$map['goods_category_pid'] = ($goods_category_pid > 0)? $goods_category_pid : 0 ;
		//文本框搜索
		if($goods_category_name){
			$map['goods_category_name'] = array('like','%'.(string)$goods_category_name.'%');
		}
		//取数据
		$list = $this->lists('ShopGoodsCategory',$map,'sort desc,goods_category_id desc','goods_category_id,goods_category_pid,goods_category_name');
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->meta_title = '商品品类列表';
		$this->assign('_list',$list);
		$this->display();
	}
	
	/**
	 * @todo 商品品类添加/修改
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function addGoodsCategory(){
		if(IS_POST){
			$this->updateGoodsCategory();
		}else{
			$id = (int)I('get.id');
			$pid = (int)I('get.pid');
			$obj = D('Admin/shopGoodsCategory');
			$up_info = $info = null;
			if($id > 0){
				$info = $obj->GoodsCategoryShow($id,'goods_category_id,goods_category_pid,goods_category_name,sort');
				if($info['goods_category_pid'] > 0){
					$up_info = $obj->GoodsCategoryShow((int)$info['goods_category_pid'],'goods_category_id,goods_category_pid,goods_category_name,sort');
				}
			}else{
				if($pid > 0){
					$up_info = $obj->GoodsCategoryShow($pid);
				}
			}
			$meta_title = '商品品类管理';
			$this->assign('info',$info);//当前
			$this->assign('up_info',$up_info);
			$this->assign('meta_title_c',$meta_title);
			$this->assign('meta_title','商品品类添加');
			$this->display('goodsCategoryAdd');
		}
	}
	
	/**
	 * @todo 添加/编辑商品品类(仅供内部调用)
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	private function updateGoodsCategory(){
		$goods_category_id = (int)I('goods_category_id');
		$obj = D('Admin/ShopGoodsCategory');
		$id = $obj->update();
		if($id === false){
			$this->error($obj->getError());
		}else{
			if($goods_category_id == 0){
				action_log('addGoodsCategory', 'ShopGoodsCategory', $id, UID);
			}else{
				action_log('editGoodsCategory', 'ShopGoodsCategory', $goods_category_id, UID);
			}
			$this->success('操作成功', Cookie('__forward__'));
		}
	}
	
	/**
	 * @todo 商品品类状态修改
	 * @param string $method 标识
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function changeGoodsCategory($method = null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if(empty($id)) {
			$this->error('请选择要操作的数据!');
		}
		$map[D('Admin/ShopGoodsCategory')->getPk()] = array('in',$id);
		switch (strtolower($method) ){
			case 'forbid':
				$this->forbid('ShopGoodsCategory', $map );
				break;
			case 'resume':
				$this->resume('ShopGoodsCategory', $map );
				break;
			case 'delete':
				$obj_Category = D('Admin/ShopGoodsCategory');
				if($obj_Category->judgeGoodsCategory($id)){
					$this->error('存在下级品类,请先删除下级品类！');
				}else{
					action_log('deleteGoodsCategory', 'ShopGoodsCategory', $id, UID);
					$this->delete('ShopGoodsCategory', $map );
				}
				break;
			default:
				$this->error('参数非法');
		}
	}
	/**
	 * @todo 订单支付状态
	 * @author fzh
	 */
	public function getOrderPayStatus(){
	    $pay_type = (string)I('get.pay_type','','htmlspecialchars,trim');
	    $out_trade_no = (string)I('get.out_trade_no','','htmlspecialchars,trim');
	    $result = null;
	    if(!empty($pay_type) && !empty($out_trade_no)){
    	    $data = array(
    	        'pay_type'=>$pay_type,
    	        'out_trade_no'=>$out_trade_no
    	    );
    	    $api_pay = D('Api/Pay','Service');
    	    $result = $api_pay->getOrderStatus($data);
    	    if(false === $result){
    	        $this->error($api_pay->getError());
    	    }
	    }
	    $this->assign('result',$result);
	    $this->assign('meta_title','订单的支付状态');
	    $this->display();
	}
	
	/**
	 * @todo    设置抢购的手机号
	 * @since 2015-10-17
	 * @author fzh
	 *  start_time 开始时间
	 *  end_time 结束时间
	 */
	public function setShopPhone() {
	    if(IS_POST){
	        $sale_shopping_goods_moblie_phone_list = I('post.sale_shopping_goods_moblie_phone_list','','htmlspecialchars');
	        S('sale_shopping_goods_moblie_phone_list',$sale_shopping_goods_moblie_phone_list,36000);
	        $this->success('修改成功');
	    }else{
	        $sale_shopping_goods_moblie_phone_list = S('sale_shopping_goods_moblie_phone_list');
	        $this->assign('sale_shopping_goods_moblie_phone_list',$sale_shopping_goods_moblie_phone_list);
	        $this->assign('meta_title','特卖神器');
	        $this->display();
	    }
	}
	
	/**
	 * @todo 参展商签到
	 * @author fzh
	 */
	public function activitySginCode(){
	    if(IS_POST){
    	    $activity_id = (int)I('activity_id');
    	    $code_id = (int)I('code_id');
    	    $shop_activity = D('ShopActivity');
    	    if(false === $shop_activity->activitySginCode($activity_id,$code_id)){
    	        $this->error($shop_activity->getError());
    	    }else{
    	        $this->success('签到成功');
    	    }
	    }else{
	        $activity_list = A('Order')->getActivityList(0);
	        $this->meta_title = '参展商签到';
	        $this->assign('activity_list',$activity_list);
	        $this->display();
	    }
	}
	
	
}